package com.engine.qz.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.engine.core.impl.Service;
import com.engine.kq.biz.KQOvertimeRulesBiz;
import com.engine.kq.biz.KQReportBiz;
import com.engine.kq.biz.KQWorkTime;
import com.engine.kq.enums.KqSplitFlowTypeEnum;
import com.engine.kq.util.KQDurationCalculatorUtil;
import com.engine.qz.service.KqDevService;
import com.engine.qz.util.QzUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.BaseBean;
import weaver.general.ThreadPoolUtil;
import weaver.general.Util;
import weaver.hrm.User;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

/**
 * @author chenwnj
 * @date 2023/12/11
 * @description
 **/
public class KqDevServiceImpl extends Service implements KqDevService {

    BaseBean bb = new BaseBean();

    @Override
    public Map<String, Object> updateCqInfo(Map<String, Object> params) {
        bb.writeLog("---updateCqInfo start---");
        Map<String, Object> result = new HashMap<>();
        try {
            String ryParam = Util.null2String(params.get("name"));
            String startDate = Util.null2String(params.get("startdate"));
            String endDate = Util.null2String(params.get("enddate"));
            String today = DateUtil.format(new Date(), "yyyy-MM-dd");
            if (org.apache.commons.lang3.StringUtils.isEmpty(endDate)) {
                endDate = today;
            }
            if (org.apache.commons.lang3.StringUtils.isEmpty(startDate)) {
                startDate = today;
            }
            QzUtil qzUtil = new QzUtil();
            List<String> allDates = qzUtil.getAllDates(startDate, endDate);
            RecordSet rs = new RecordSet();
            List<String> userIds = new ArrayList<>();
            Map<String, String> statusMap = new HashMap<>();
            if (org.apache.commons.lang3.StringUtils.isEmpty(ryParam)) {
                bb.writeLog("1111.");
                rs.executeQuery("select id, status from hrmresource");
                while (rs.next()) {
                    String idT = rs.getString("id");
                    if (StringUtils.isEmpty(idT)) {
                        continue;
                    }
                    String statusT = rs.getString("status");
                    if (StringUtils.isEmpty(statusT)) {
                        statusT = "0";
                    }
                    userIds.add(idT);
                    statusMap.put(idT, statusT);
                }
            } else {
                List<String> ryParamTemp = Arrays.asList(ryParam.split(","));
                userIds.addAll(ryParamTemp);
                rs.executeQuery("select id, status from hrmresource");
                while (rs.next()) {
                    String idT = rs.getString("id");
                    if (StringUtils.isEmpty(idT)) {
                        continue;
                    }
                    if (!ryParamTemp.contains(idT)) {
                        continue;
                    }
                    String statusT = rs.getString("status");
                    if (StringUtils.isEmpty(statusT)) {
                        statusT = "0";
                    }
                    statusMap.put(idT, statusT);
                }
            }
            bb.writeLog("userIds:" + userIds);
            if (CollectionUtils.isEmpty(userIds) || CollectionUtils.isEmpty(allDates)) {
                bb.writeLog("no user or date.");
                result.put("mes", "没有数据需要更新！");
                result.put("code", "200");
                return result;
            }

            //获取休息班班次
            List<String> xxShiftList = new ArrayList<>();
            rs.executeQuery("select bc from uf_kqxxbcb ");
            while (rs.next()) {
                String bc = Util.null2String(rs.getString("bc"));
                if (StringUtils.isNotBlank(bc)) {
                    xxShiftList.add(bc);
                }
            }

            // 获取离职日期
            Map<String, String> lzMap = new HashMap<>();
            rs.execute("select resourceid, changedate from HRMSTATUSHISTORY  where type_n = 5");
            while (rs.next()) {
                String resourceid = rs.getString("resourceid");
                String changedate = rs.getString("changedate");
                if (StringUtils.isEmpty(resourceid) || StringUtils.isEmpty(changedate)) {
                    continue;
                }
                lzMap.put(resourceid, changedate);
            }

            // 获取存在请假或者出差或者公出的人员及日期
            Set<String> existSet = qzUtil.getHrmRqSet(startDate, endDate);
            bb.writeLog("handleCQData start.");
            for (String userId : userIds) {
                for (String kqDate : allDates) {
                    ThreadPoolUtil.getThreadPool("HandleCQDataB", "5").execute(() -> {
                        qzUtil.handleCQData(userId, kqDate, xxShiftList, existSet, lzMap, statusMap);
                    });
                }
            }
            bb.writeLog("handleCQData end.");
        } catch (Exception e) {
            bb.writeLog("updateCqInfo error:" + e.getMessage());
            result.put("mes", "系统错误，请联系管理员！");
            result.put("code", "400");
            return result;
        }
        result.put("mes", "已开始计算，请稍等！");
        result.put("code", "200");
        return result;
    }

}
